package com.example.lab.controller;

import com.example.lab.entity.Colleges;
import com.example.lab.result.PageResult;
import com.example.lab.result.Result;
import com.example.lab.service.ICollegesService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *@Author: 布莱恩
 *@Date: 2025/6/17 17:32
 *@Description:
 **/
@RestController
@RequestMapping("/api/colleges")
@RequiredArgsConstructor
public class CollegeController {

    private final ICollegesService collegeService;

    @GetMapping
    public Result<PageResult<Colleges>> getColleges(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        PageResult<Colleges> result = collegeService.getColleges(page, limit);
        return Result.success(result);
    }

    @GetMapping("/{collegeId}")
    public Result<Colleges> getCollegeById(@PathVariable Integer collegeId) {
        Colleges college = collegeService.getCollegeById(collegeId);
        return Result.success(college);
    }

    @PostMapping
    public Result<Integer> createCollege(@RequestBody Colleges college) {
        boolean success = collegeService.createCollege(college);
        if (success) {
            return Result.success(college.getCollegeId());
        }
        return Result.fail("学院创建失败");
    }

    @PutMapping("/{collegeId}")
    public Result<Void> updateCollege(
            @PathVariable Integer collegeId,
            @RequestBody Colleges college) {
        college.setCollegeId(collegeId);
        boolean success = collegeService.updateCollege(collegeId, college);
        if (success) {
            return Result.success();
        }
        return Result.fail("学院更新失败");
    }

    @DeleteMapping("/{collegeId}")
    public Result<Void> deleteCollege(@PathVariable Integer collegeId) {
        boolean success = collegeService.deleteCollege(collegeId);
        if (success) {
            return Result.success();
        }
        return Result.fail("学院删除失败");
    }

}
